﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
namespace DesktopLauncher
{
    public partial class FrmSetting : Form
    {
        public FrmSetting()
        {
            InitializeComponent();
        }

        private void txtFolderPath_Click(object sender, EventArgs e)
        {
            btnSelectFolder.PerformClick();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            AddUpdateAppSettings("FOLDER_PATH", txtFolderPath.Text);
            Application.Exit();         
        }

        private void FrmSetting_Load(object sender, EventArgs e)
        {
            txtFolderPath.Text = System.Configuration.ConfigurationManager.AppSettings["FOLDER_PATH"];

            StringBuilder aboutStrBuilder = new StringBuilder();
            aboutStrBuilder.Append(@"说明:
桌面图标太多太乱,为什么不藏起来一部分?
使用方式很简单,在创建一个文件夹,然后把不常用的图标扔进去,再设置软件指向这个文件夹就可以了.
把程序固定到任务栏,快捷加倍");
            aboutStrBuilder.AppendLine();
            aboutStrBuilder.Append("版本: ");
            aboutStrBuilder.Append(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
            aboutStrBuilder.AppendLine();
            aboutStrBuilder.AppendLine("https://gitee.com/somereason/desktop_app_launcher");
            txtAbout.Text = aboutStrBuilder.ToString() ;
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            if (folderSelector.ShowDialog() == DialogResult.OK)
            {
                txtFolderPath.Text = folderSelector.SelectedPath;
            }
        }

        static void AddUpdateAppSettings(string key, string value)
        {
            try
            {
                var configFile = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error writing app settings");
            }
        }
    }
}
